import machine
from machine import ADC, Pin,I2C, deepsleep
from machine import RTC, WDT
import utime
import esp32
import PFC8563

import lcd12864
from sht31 import *
from sht20 import *

xj=[
b'\x40\x40\x42\x42\x42\x42\x42\xFE\x42\x42\x42\x42\x42\x40\x40\x00\x80\x80\x40\x20\x10\x0C\x03\x00\x03\x0C\x10\x20\x40\x80\x80\x00',#"天",0
b'\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',#"一",1
b'\x00\x00\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x00\x00\x00\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x00',#"二",2
b'\x00\x04\x84\x84\x84\x84\x84\x84\x84\x84\x84\x84\x84\x04\x00\x00\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x00',#"三",3
b'\x00\xFC\x04\x04\x04\xFC\x04\x04\x04\xFC\x04\x04\x04\xFC\x00\x00\x00\x7F\x28\x24\x23\x20\x20\x20\x20\x21\x22\x22\x22\x7F\x00\x00',#"四",4
b'\x00\x02\x42\x42\x42\xC2\x7E\x42\x42\x42\x42\xC2\x02\x02\x00\x00\x40\x40\x40\x40\x78\x47\x40\x40\x40\x40\x40\x7F\x40\x40\x40\x00',#"五",5
b'\x20\x20\x20\x20\x20\x20\x21\x22\x2C\x20\x20\x20\x20\x20\x20\x00\x00\x40\x20\x10\x0C\x03\x00\x00\x00\x01\x02\x04\x18\x60\x00\x00',#"六",6
b'\x00\x00\x00\xBE\x2A\x2A\x2A\xEA\x2A\x2A\x2A\x3E\x00\x00\x00\x00\x00\x44\x42\x49\x49\x49\x49\x7F\x49\x49\x49\x49\x41\x40\x00\x00',#"星",7
b'\x00\x04\xFF\x24\x24\x24\xFF\x04\x00\xFE\x22\x22\x22\xFE\x00\x00\x88\x48\x2F\x09\x09\x19\xAF\x48\x30\x0F\x02\x42\x82\x7F\x00\x00',#"期",8
]

adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)    # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
adc.width(ADC.WIDTH_12BIT)  
SHT31_address       =   68
i2c = I2C(1,scl=Pin(18), sda=Pin(19),freq=400000)
# sht31m(i2c)
# utime.sleep_ms(50)
# (t,h)=(sht31r(i2c))
# a=adc.read()
# print(a)
# print(t,h,a*6.5362/4096)   
# IIC256.lprint("-%.2fC %.2f%%   "%(t,h),0,8)
wake1 = Pin(27, mode = Pin.IN)

#level parameter can be: esp32.WAKEUP_ANY_HIGH or esp32.WAKEUP_ALL_LOW
#

#your main code goes here to perform a task

# while 1:
#     utime.sleep(3)
#     print('Going to sleep now')
#     utime.sleep_ms(1)
#     machine.lightsleep()
#     print("wakeup")


def showFast():
    leds=machine.Pin(14,machine.Pin.OUT,Pin.PULL_HOLD)
    leds.off()
    k=0
    for i in range(60):
        if(wake1.value()==1):
            k=k+1
            # IIC256.lprint("%02d"%(k),64,14)
        else:
            k=0
            # IIC256.lprint("      ",64,14)
        if k==3:
            # IIC256.lprint("sync",64,14)
            PFC8563.sync()
            # IIC256.lprint("      ",64,14)


        


def gettime():
        da=i2c.readfrom_mem(81,2,8)
        s=((da[0]>>4) * 10) + (da[0]&0x0f)
        m=((da[1]>>4) * 10) + (da[1]&0x0f)
        h=((da[2]>>4) * 10) + (da[2]&0x0f)
        d=((da[3]>>4) * 10) + (da[3]&0x0f)
        w=((da[4]>>4) * 10) + (da[4]&0x0f)
        mo=((da[5]>>4) * 10) + (da[5]&0x0f)
        y=((da[6]>>4) * 10) + (da[6]&0x0f)
        return (s,m,h,d,w,mo,y)

def gett():
    i2c.writeto(SHT20_address,bytearray(T_measurement))
    utime.sleep_ms(100)
    buf=i2c.readfrom(SHT20_address,3)
    t0=buf[0]*256+buf[1]
    i2c.writeto(SHT20_address,bytearray(RH_measurement))
    utime.sleep_ms(80)
    buf=i2c.readfrom(SHT20_address,3)
    t=buf[0]*256+buf[1]
    sht20rt=(t0/65536*175.72)-46.85
    sht20rh=(t/65536*125)-6
    IIC256.print32("%.2fC %.2f%%   "%(sht20rt,sht20rh),0,8)
    v=adc.read()
    print(v)
    IIC256.print32("%.2fV   "%(v*7.0414/4096),0,12)


if __name__=="__main__":

    rtc = RTC()
    i2c = I2C(1,scl=Pin(18), sda=Pin(19), freq=400000)

    # PFC8563.sync()
    lcd12864.lcdInit()
    t=rtc.datetime()
    print("{0:0>2d}:{1:0>2d}:{2:0>2d}".format(t[4],t[5],t[6]))
    print(t)


    # wdt = WDT(timeout=3000)
    k=0
    k1=0
    k2=5
    k3=k2
    while 1:
        t=gettime()
        print(t)
        lcd12864.st(t[2],t[1],t[6]+2000,t[5],t[3],t[4])
        lcd12864.print8("%02d"%(int(t[0])),80,2)
        if(k1>=2):
            #sht31m(i2c)
            #utime.sleep_ms(50)
            #(rt,rh)=(sht31r(i2c))
            #lcd12864.print8("sht31: %.2fC %.2f%%   "%(rt,rh),0,5)
            v=adc.read()
            lcd12864.print8("%04d %.2fV"%(v,v*7.06/4096),64,4)
            # IIC256.print32("%.2fV   "%(v*7.1413/4096),0,12)
            t=sht20C()
            lcd12864.print8("sht20: %.2fC %.2f%%   "%(t[0],t[1]),0,5)
            k1=0
            print("c")
        else:
            k1=k1+1
            print(k1)

        

        if(wake1.value()==1):
            
            k=k+1
            lcd12864.print8("%02d"%(k),0,7)
            k3=k2
        else:
            if k==1:
                # leds.off()
                k=0
                lcd12864.print8("      ",0,7)
            if k==3:
                lcd12864.print8("sync",0,7)
                PFC8563.sync()
                k=0
                lcd12864.print8("      ",0,7)
            k=0
            lcd12864.print8("      ",0,7)

        # if(k3>0):
        #     leds=machine.Pin(14,machine.Pin.OUT,machine.Pin.PULL_HOLD,value=1)
        #     leds.off()
        #     k3=k3-1
        #     print("k3{%02d}"%(k3))
        # else:
        #     k3=0
        #     #leds.on()
        #     leds.off()
        # utime.sleep(3)
        # wdt.feed()
        # utime.sleep_ms(10)
        # t=rtc.datetime()
        # utime.sleep_us(5000000-t[7])
        # machine.lightsleep()
        v=adc.read()
        if(v<2200):
            # leds.on()

            #sht31m(i2c)
            t=sht20C()
            lcd12864.print8("    ",80,2)
            #utime.sleep_ms(50)
            #(rt,rh)=(sht31r(i2c))
            #lcd12864.print8("sht31: %.2fC %.2f%%   "%(rt,rh),0,5)
            v=adc.read()
            lcd12864.print8("%04d %.2fV"%(v,v*7.06/4096),64,4)
            # IIC256.print32("%.2fV   "%(v*7.1413/4096),0,12)
            
            lcd12864.print8("sht20: %.2fC %.2f%%   "%(t[0],t[1]),0,5)
            k1=0
            #print("c")
            t=gettime()
            machine.deepsleep(60000-(t[0]*1000))
        else:
            # leds.off()
            t=rtc.datetime()
            # utime.sleep_us(1000000-t[7])
            machine.lightsleep(int((1000000-t[7])/1000))